package com.cpe.transfer.appoint;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.cpe.data.domain.appoint.Appoint;
import com.cpe.data.domain.appoint.AppointInfo;
import com.cpe.data.domain.appoint.AppointedChild;
import com.cpe.data.domain.appoint.AppointingChild;
import com.cpe.data.domain.member.appoint.MemberAppoint;
import com.cpe.repository.mapper.member.MemberAppointMapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;

@Component
public class AppointTransfer {
	@Resource
	private MemberAppointMapper memberAppointMapper;

	/**
	 * 实体-->响应
	 */
	public AppointInfo toInfo(Appoint appoint, String memberId) {
		Assert.notNull(appoint);
		AppointInfo info = new AppointInfo();
		BeanUtils.copyProperties(appoint, info);

		MemberAppoint query = new MemberAppoint();
		query.setMemberId(memberId);
		query.setAppointId(appoint.getTitle());
		query.setStatus(3);
		List<MemberAppoint> memberAppoints = memberAppointMapper.findByMemberIdAndAppointIdAndStatusNot(query);
		memberAppoints.forEach(memberAppoint -> {
			if (memberAppoint.getStatus() == 1) {
				AppointingChild child = new AppointingChild();
				child.setId(memberAppoint.getStudentId());
				child.setName(memberAppoint.getStudentName());
				info.getAppointingChilds().add(child);
			} else if (memberAppoint.getStatus() == 2) {
				AppointedChild child = new AppointedChild();
				child.setId(memberAppoint.getStudentId());
				child.setName(memberAppoint.getStudentName());
				child.setStartTime(memberAppoint.getStartTime());
				info.getAppointedChilds().add(child);
			}
		});

		return info;
	}

	/**
	 * 实体分页-->响应分页
	 */
	public PageInfo<AppointInfo> toPageInfo(List<Appoint> appoints, String memberId) {
		Assert.notNull(appoints);
		PageInfo<Appoint> pageInfo = new PageInfo<>(appoints);
		PageInfo<AppointInfo> pageResp = new PageInfo<>();
		BeanUtils.copyProperties(pageInfo, pageResp);
		List<AppointInfo> infoList = Lists.newArrayList();
		for (Appoint appoint : appoints) {
			infoList.add(toInfo(appoint, memberId));
		}
		pageResp.setList(infoList);
		return pageResp;
	}
}
